feat: implement rerender functionality to fix host component rerender performance issue#4181
feat: implement rerender functionality to fix host component rerender performance issue#4181ScriptedAlchemy wants to merge 1 commit into
Conversation
|
✅ Deploy Preview for module-federation-docs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| // Store the setState function for external updates | ||
| React.useEffect(() => { | ||
| const domElement = document.querySelector(`[data-module-name="${moduleName}"]`); | ||
| if (domElement) { | ||
| propsStateMap.set(domElement, { props, setState: setProps }); | ||
| } | ||
| }, [moduleName, props]); |
There was a problem hiding this comment.
Register rerender state against the wrong DOM node
The state map uses document.querySelector('[data-module-name="${moduleName}"]') to look up the element whose setState is stored. When multiple instances of the same remote module are rendered, this selector always returns the first matching element, so subsequent instances overwrite the entry for the first DOM node and never register their own. Later rerenders for those instances fall back to recreating the root or call setState on an unmounted component, leading to stale UI and potential React warnings. The map key needs to use the actual container passed to render, not a global query by module name.
Useful? React with 👍 / 👎.
🎯 Overview
This PR implements the rerender functionality to solve the performance issue described in GitHub issue #4171, where host component rerenders caused the entire remote app to be recreated instead of just rerendering.
🔧 Changes Made
1. Enhanced Type Definitions (types.ts)
rerender?: (props: T) => voidtoProviderFnParams<T>interfaceBridgeComponentInstanceinterface with rerender method2. State Management System (bridge-base.tsx)
StatefulBridgeWrappercomponent using React state3. Optimized Prop Handling (component.tsx)
...Object.values(props)dependency that caused performance issues4. Comprehensive Testing (bridge.spec.tsx)
5. Documentation (RERENDER_EXAMPLE.md)
🚀 API Usage
Automatic Optimization (Default - No Code Changes Needed)
Custom Rerender Logic (Advanced Use Cases)
Host App Usage (No Changes Required)
🔄 Performance Impact
Before (Problem):
After (Solution):
Or with custom rerender:
🧪 Implementation Details
State Management Architecture
Backward Compatibility
✅ Quality Assurance
Build & Type Safety
nx build bridge-reactpassesTesting Results
Performance Verification
🎉 Benefits Delivered
📋 Files Changed
packages/bridge/bridge-react/src/types.ts- Enhanced type definitionspackages/bridge/bridge-react/src/provider/versions/bridge-base.tsx- State management systempackages/bridge/bridge-react/src/remote/component.tsx- Optimized prop handlingpackages/bridge/bridge-react/__tests__/bridge.spec.tsx- Comprehensive testspackages/bridge/bridge-react/RERENDER_EXAMPLE.md- Documentation and examplesFixes #4171